package com.play.ballen.im;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMUserInfo;
import com.hyphenate.easeui.constants.EaseConstant;
import com.mw.plp.uim.common.utils.ToastUtils;
import com.play.ballen.R;
import com.play.ballen.data.PImGroupStatus;
import com.play.ballen.databinding.ActivityChatSettingBinding;
import com.play.ballen.network.ApiRepository;
import com.play.ballen.network.BallenRxHelper;
import com.yuven.appframework.network.RxSchedulerHelper;
import com.yuven.baselib.component.BaseMVVMActivity;
import com.yuven.baselib.component.viewmodel.BaseViewModel;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

public class PvtChatSettingActivity extends BaseMVVMActivity<BaseViewModel, ActivityChatSettingBinding> {
    private String conversationId;
    public static void actionStart(Context context, String conversationId) {
        Intent intent = new Intent(context, PvtChatSettingActivity.class);
        intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        context.startActivity(intent);
    }

    @Override
    protected void initIntent(Intent intent) {
        conversationId = intent.getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);
    }
    @Override
    protected int layoutId() {
        return R.layout.activity_chat_setting;
    }

    @Override
    protected Class<BaseViewModel> viewModelClass() {
        return BaseViewModel.class;
    }

    @Override
    protected void initView() {
        dataBinding.ivBtnBack.setOnClickListener(v -> onBackPressed());
        dataBinding.tvBtnClear.setOnClickListener(v -> {
            EMClient.getInstance().chatManager().deleteConversation(conversationId,true);
            ToastUtils.showSuccessToast("清除成功");
            finish();
        });
    }

    @Override
    protected void initData() {
        getPvtStatus();
    }

    public void setUserInfo(EMUserInfo userInfo){
        if (userInfo!=null){
            dataBinding.tvName.setText(userInfo.getNickname());
        }else {
            dataBinding.tvName.setText("*****");
        }
    }
    public void setBlackStatus(boolean isBlack){
        dataBinding.swBtn.setOnCheckedChangeListener(null);
        dataBinding.swBtn.setChecked(isBlack);
        dataBinding.swBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateBlackStatus(isChecked);
        });
    }
    private void getPvtStatus(){
        new CompositeDisposable()
                .add(new ApiRepository()
                        .getPrivateGroupStatus(Arrays.asList(conversationId))
                        .onErrorResumeNext(new BallenRxHelper.HttpErrorHandleFunc())
                        .compose(BallenRxHelper.transformResponse())
                        .compose(RxSchedulerHelper.io_main())
                        .subscribe(s -> {
                            addGroupInfo((Map<String, List<PImGroupStatus>>) s);
                        }, err -> {
                            addGroupInfo(null);
                        }));
    }
    private PImGroupStatus mineStatus;
    private PImGroupStatus otherStatus;
    private void addGroupInfo(Map<String, List<PImGroupStatus>> res) {
        otherStatus = PvtTools.getOtherStatus(res.get(conversationId));
        mineStatus=PvtTools.getMineStatus(res.get(conversationId));

        if (otherStatus != null && otherStatus.is_block) {
            setBlackStatus(true);
        } else {
            setBlackStatus(false);
        }
        if (otherStatus!=null&&mineStatus.anonymous){
            setUserInfo(null);
        }else{
            setUserInfo(null);
        }
    }


    private void updateBlackStatus(boolean isBlack){
        if (isBlack){


        }else{

        }


    }


}
